/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-02-05
*/
package com.rzico.basics.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.CouponCodeGift;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.CouponCodeGiftService;
import com.rzico.basics.service.CouponCodeService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提货券转账记录控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-02-05
 */
@Api(description = "提货券转账记录接口")
@RestController
@RequestMapping("/member/couponCodeGift")
public class CouponCodeGiftMemberController extends BaseController {

    @Autowired
    private CouponCodeGiftService couponCodeGiftService;

    @Autowired
    private CouponCodeService couponCodeService;

    @Autowired
    private SysSequenceService sysSequenceService;

    @Autowired
    private MemberService memberService;

    /**
     * 我送出的提货券
     *
     * @return
    */
    @ApiOperation("我送出的提货券")
    @GetMapping("/memberList")
    public CommResult<CouponCodeGift> memberList(Date startDate, Date endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }


        Member member = memberService.getCurrent();
        params.put("memberId",member.getId());

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CouponCodeGift> list = couponCodeGiftService.selectList(params);
        for (CouponCodeGift gift:list) {
           gift.setCouponCodeVo(couponCodeService.selectVoOne(gift.getCouponCodeId()));
        }
        PageResult<CouponCodeGift> pageResult = new PageResult<CouponCodeGift>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }


    /**
     * 我收到的提货券
     *
     * @return
     */
    @ApiOperation("我收到的提货券")
    @GetMapping("/receiverList")
    public CommResult<CouponCodeGift> receiverList(Date startDate, Date endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
            params.put("startDate", startDate);
        }
        if (endDate!=null){
            params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        Member member = memberService.getCurrent();
        params.put("receiverId",member.getId());

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CouponCodeGift> list = couponCodeGiftService.selectList(params);
        for (CouponCodeGift gift:list) {
            gift.setCouponCodeVo(couponCodeService.selectVoOne(gift.getCouponCodeId()));
        }
        PageResult<CouponCodeGift> pageResult = new PageResult<CouponCodeGift>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 保存提货券转账记录
     *
     * @param couponCodeGift
     * @return
     */
    @ApiOperation("保存提货券转账记录")
    @PostMapping("/save")
    public CommResult<CouponCodeGift> save(@RequestBody List<CouponCodeGift> couponCodeGift) {
        Member member = memberService.getCurrent();
        for (CouponCodeGift gift:couponCodeGift) {
            gift.setCreateDate(new Date());
            gift.setMemberId(member.getId());
            gift.setStatus(0);
            gift.setEnterpriseId(member.getEnterpriseId());
        }

        try {
            couponCodeGiftService.save(couponCodeGift);
        } catch (Exception e) {
            return CommResult.error(e.getMessage());
        }
        return CommResult.success();

    }

    /**
     * 确认接收提货券
     *
     * @param couponCodeGift
     * @return
     */
    @Log(desc = "确认接收提货券", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("确认接收提货券")
    @PostMapping("/confirm")
    public CommResult<CouponCodeGift> confirm(@RequestBody CouponCodeGift couponCodeGift) {
        couponCodeGift = couponCodeGiftService.selectByPrimaryKey(couponCodeGift.getId());
        if (!couponCodeGift.getStatus().equals(0)) {
            return CommResult.error("不能确认");
        }
        String sn = sysSequenceService.generate("couponCode");
        couponCodeGiftService.confirm(couponCodeGift,sn);

        CouponCodeGift result = couponCodeGiftService.findById(couponCodeGift.getId());
        return CommResult.success(result);
    }



    /**
     * 撤消提货券
     *
     * @param couponCodeGift
     * @return
     */
    @Log(desc = "撤消提货券", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("撤消提货券")
    @PostMapping("/cancel")
    public CommResult<CouponCodeGift> cancel(@RequestBody CouponCodeGift couponCodeGift) {
        couponCodeGift = couponCodeGiftService.selectByPrimaryKey(couponCodeGift.getId());
        if (!couponCodeGift.getStatus().equals(0)) {
            return CommResult.error("不能取消");
        }
        couponCodeGiftService.cancel(couponCodeGift);

        CouponCodeGift result = couponCodeGiftService.findById(couponCodeGift.getId());
        return CommResult.success(result);
    }

}
